package com.hl.sfzy.controller;

import com.hl.sfzy.entity.LogonUser;
import com.hl.sfzy.service.ReplaceBindPhoneService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: sfzy
 * @description: 更换绑定手机
 * @author: Syy
 * @create: 2024-09-10 11:38
 **/
@RestController
@RequestMapping("/replace")
public class ReplaceBindPhoneController {
    @Autowired
    private ReplaceBindPhoneService replaceBindPhoneService;

    //更换绑定手机接口1
    @GetMapping("/select")
    public R replace(String username) {
        LogonUser logonUser = replaceBindPhoneService.selectByUsername(username);
        return R.success(logonUser);
    }

    //更换绑定手机接口2
    //先根据手机号查询用户
    @GetMapping("/select2")
    public R replace2(String phone) {
        List<LogonUser> list = replaceBindPhoneService.selectByPhone(phone);
        if (list != null) {
            for (LogonUser logonuser : list) {
                if (logonuser.getRole() == 3) {
                    return R.success(logonuser);
                }
            }
        }
        return null;
    }

    //根据id修改用户手机号实现更换绑定手机
    @PostMapping("/update")
    public R replace3(String id, String phone) {
        System.out.println(id);
        System.out.println(phone);
        int i = replaceBindPhoneService.replaceBindPhone(id, phone);
        if (i == 1) {
            return R.success(i);
        } else return R.failure(i);
    }
}
